//
//  FWPageScrollerController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/16.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWPageScrollerController.h"
#import "FWPageButton.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
const CGFloat btnW = 80;

@interface FWPageScrollerController ()<UIScrollViewDelegate, PageButtonDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *titleScroView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentScroView;
@property (weak, nonatomic) UIButton *seelectBtn;
@property (assign, nonatomic) NSInteger selectIndex;
@property (weak, nonatomic) UIView *buttomView;
@end

@implementation FWPageScrollerController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    return [super initWithNibName:@"FWPageScrollerController" bundle:nibBundleOrNil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self initView];
        [self addTitleButtomView];
        [self scrollViewDidEndScrollingAnimation:self.contentScroView];
    });
}

- (void)initView{
    
    CGFloat btnY = 0;
    CGFloat btnH = self.titleScroView.height - 2;
    NSInteger count = self.childViewControllers.count;
    for (int index = 0; index < count; index++) {
        UIViewController *childVC = self.childViewControllers[index];
        FWPageButton *btn = [[FWPageButton alloc] init];
        btn.title = childVC.title;
        btn.delegate = self;
        if (index == 0) btn.scal = 1.0;
        CGFloat btnX = btnW * index;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self.titleScroView addSubview:btn];
    }
    self.titleScroView.contentSize = CGSizeMake(btnW *count, 0);
    self.contentScroView.contentSize = CGSizeMake(kScreenW * count, 0);
    self.titleScroView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.97];
}

- (void)addTitleButtomView{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    view.size = CGSizeMake(btnW*0.5, 2);
    FWPageButton *btn = self.titleScroView.subviews[0];
    view.center = CGPointMake(btn.center.x, btn.height);
    [self.titleScroView addSubview:view];
    self.buttomView = view;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];
}

#pragma mark PageButton delegate
- (void)onClickPageButton:(FWPageButton *)pageBtn index:(NSInteger)index{
    if (self.selectIndex == index) {
        [[NSNotificationCenter defaultCenter] postNotificationName:FWTabBarDidSelectNotification object:nil];
    }
    self.selectIndex = index;
    [self.contentScroView setContentOffset:CGPointMake(index * kScreenW, 0) animated:YES];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    FWPageButton *btn = self.titleScroView.subviews[index];
    _seelectBtn.selected = NO;
    btn.selected = YES;
    _seelectBtn = btn;
    
    [UIView animateWithDuration:0.3 animations:^{
        _buttomView.center = CGPointMake(btn.center.x, self.titleScroView.height - 2);
    }];
    
    for (UIView *view in self.titleScroView.subviews) {
        if ([view isKindOfClass:[FWPageButton class]]) {
            FWPageButton *pageBtn = (FWPageButton *)view;
            if (pageBtn != btn) {
                pageBtn.scal = 0;
            }
        }
    }
    
    CGPoint point = self.titleScroView.contentOffset;
    point.x = btn.center.x - kScreenW * 0.5;
    CGFloat maxX = self.titleScroView.contentSize.width - kScreenW;
    if (point.x < 0) {
        point.x = 0;
    } else if (point.x > maxX){
        point.x = maxX;
    }
    [self.titleScroView setContentOffset:point animated:YES];
    
    UIViewController *childVC = self.childViewControllers[index];
    
    if ([childVC isViewLoaded]) {
        return;
    }
    childVC.view.frame = CGRectMake(scrollView.contentOffset.x, 0, scrollView.width, scrollView.height);   
    [self.contentScroView addSubview:childVC.view];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger leftPage = scrollView.contentOffset.x / kScreenW;
    NSInteger rightPage = leftPage + 1;
    
    CGFloat rightScal = scrollView.contentOffset.x / kScreenW - leftPage;
    CGFloat leftScal = 1 - rightScal;
    
    FWPageButton *leftBtn = self.titleScroView.subviews[leftPage];
    leftBtn.scal = leftScal;
    
    if (rightPage > self.contentScroView.subviews.count - 1) {
        return;
    }

    FWPageButton *rightBtn = self.titleScroView.subviews[rightPage];
    rightBtn.scal = rightScal;
}

@end
